//
//  QainliProtocolWebPage.swift
//  todoios
//
//  Created by qianli on 2025/5/22.
//

import Foundation
import UIKit
import WebKit

class QainliProtocolWebPage: QianliParentNaviVC, WKUIDelegate, WKNavigationDelegate {
    // MARK: - 属性
    private var _url: URL?
    private var _webView: WKWebView!
    
    private var QQyGtPYINznEtspj: String?
    private var IB5he4Yk80F28O9P: UIProgressView=UIProgressView(progressViewStyle: .default)
//    private var Xq8nWGQDfj2WNOjw: UIToolbar!
    
    // MARK: - 初始化
    init(url: URL,title:String) {
        self._url = url
        self.QQyGtPYINznEtspj = title
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func GHL4Rhb91XYBer27() {
        // 进度条
        view.addSubview(self.IB5he4Yk80F28O9P)
        self.IB5he4Yk80F28O9P.progress = 0.0
        self.IB5he4Yk80F28O9P.tintColor = .systemBlue
        self.IB5he4Yk80F28O9P.translatesAutoresizingMaskIntoConstraints = false
        self.IB5he4Yk80F28O9P.frame = CGRect(x: 0, y: topSpace, width: qianli_pm_width, height: 1)
        
        if let naviTitle = self.QQyGtPYINznEtspj{
            self.zdMpsGrNncVEHdYA(title: naviTitle)
        }
        
//        ldG9MW3IWj9Uw254()
        self.perform(#selector(ldG9MW3IWj9Uw254), with: nil, afterDelay: 0.5)
    }
    
    @objc private func ldG9MW3IWj9Uw254() {
        let config = WKWebViewConfiguration()
        _webView = WKWebView(frame: CGRect(x: 0, y: topSpace+1, width: qianli_pm_width, height: qianli_pm_height-topSpace), configuration: config)
        _webView.uiDelegate = self
        _webView.navigationDelegate = self
        view.addSubview(_webView)
        
        // 监听加载进度
        _webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
        Wut2nyndHa1XggBq()
        
    }
    
    private func Wut2nyndHa1XggBq() {
        guard let url = _url else {
            rYNPOk8a6zkMoUbe(message: "无效的URL")
            return
        }
        
        let request = URLRequest(url: url)
        _webView.load(request)
    }
    
//    @objc private func nY9xaV5K5NkhJt9z() {
//        if _webView.canGoBack {
//            _webView.nY9xaV5K5NkhJt9z()
//        }
//    }
//    
//    @objc private func cv9MnvBRfVTzE0hp() {
//        if _webView.canGoForward {
//            _webView.cv9MnvBRfVTzE0hp()
//        }
//    }
//    
//    @objc private func WuqtDSBy8liOWtwF() {
//        _webView.reload()
//    }
    
    // MARK: - 提示框
    private func rYNPOk8a6zkMoUbe(message: String) {
        let alert = UIAlertController(title: LocalizedString("Infomation"), message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: LocalizedString("SURE"), style: .default))
        present(alert, animated: true)
    }
    
    // MARK: - KVO监听
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == #keyPath(WKWebView.estimatedProgress) {
            self.IB5he4Yk80F28O9P.progress = Float(_webView.estimatedProgress)
            
            // 加载完成后隐藏进度条
            if _webView.estimatedProgress >= 1.0 {
                UIView.animate(withDuration: 0.3) {
                    self.IB5he4Yk80F28O9P.alpha = 0.0
                }
            } else {
                self.IB5he4Yk80F28O9P.alpha = 1.0
            }
        }
    }
    
    // MARK: - WKNavigationDelegate
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        rYNPOk8a6zkMoUbe(message: "\(LocalizedString("Load Failed")): \(error.localizedDescription)")
    }
    
//    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//        title = webView.title ?? ""
//    }
    
    // MARK: - 清理
    deinit {
        _webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress))
    }
}
